#include "OfferPlayerMenu.h"
#include "PrintUtilities.h"
#include "StringUtilities.h"
#include "MenuManager.h"
#include "User.h"
#include "ServiceProvider.h"
#include "UserManager.h"
#include "UserSelectionMenu.h"
#include "People.h"
#include "Team.h"
#include "AuctionManager.h"

OfferPlayerMenu::OfferPlayerMenu(MenuManager* const i_menuManager)
	: Menu(i_menuManager)
	, m_userTeam(0)
{
}

void OfferPlayerMenu::OnEnter()
{
	m_phase = 0;
	m_readyToGo = false;

	m_gift = 0;

#ifndef DARIO
	m_salary = 0;
	m_months = 0;
	m_penalty = 0;
#endif

	if(m_userTeam == 0)
	{
		const UserSelectionMenu* const usMenu = static_cast<UserSelectionMenu*>(GetMM()->GetUserSelectionMenu());
		m_userTeam = GetSP()->GetUserMgr().GetUserList().at(usMenu->GetUserSelected())->m_team;
	}
}

void OfferPlayerMenu::OnExit()
{
	m_userTeam = 0;
}

void OfferPlayerMenu::Draw() const
{
	using STD::cout;
	using STD::endl;

	cout << Print::Title(" OFFER PLAYER MENU:") << endl << endl;

	bool isPlayer = m_player->IsPlayer();
	bool jumpError = false; // LOL I'd need a GOTO ^_^

	cout << m_userTeam->GetName() << " Is trying to buy " << m_player->GetName() << endl << endl;

	if(!jumpError && !isPlayer)
	{
		cout << "Trainer Offers are not yet implemented...\n";
		jumpError = true;
	}

	if(!jumpError && isPlayer && m_userTeam->GetNumPlayers() >= Team::GetParams().MAX_TEAM_PLAYERS)
	{
		cout << "You can't buy the player, your team has reached maximum players number!\n";
		jumpError = true;
	}

	if(!jumpError && !isPlayer && m_userTeam->GetNumTrainers() >= Team::GetParams().MAX_TEAM_TRAINERS)
	{
		cout << "You can't buy the trainer, your team has reached maximum trainers number!\n";
		jumpError = true;
	}

	if(!jumpError)
	{
#ifndef DARIO
		switch(m_phase)
		{
		case ENTRYGIFT:
			cout << "Enter the Initial Gift (TODO: unused at the moment): ";
			break;

		case SALARY:
			cout << "The Initial Gift (TODO: unused at the moment) is " << m_gift << endl;
			cout << "\nNow enter The Monthly Salary (TODO: salary per match at the moment): ";
			break;

		case MONTHS:
			cout << "The Initial Gift (TODO: unused at the moment) is " << m_gift << endl;
			cout << "The Monthly Salary (TODO: salary per match at the moment) is " << m_salary << endl;
			cout << "\nNow enter The Months Length of the contract (TODO: years at the moment): ";
			break;

		case PENALTY:
			cout << "The Initial Gift (TODO: unused at the moment) is " << m_gift << endl;
			cout << "The Monthly Salary (TODO: salary per match at the moment) is " << m_salary << endl;
			cout << "The Months Length (TODO: years at the moment) is " << m_months << endl;
			cout << "\nNow enter The Monthly Penalty for premature interruption (TODO: unused at the moment): ";
			break;

		case NUM_PHASES:
			cout << "The Initial Gift (TODO: unused at the moment) is " << m_gift << endl;
			cout << "The Monthly Salary (TODO: salary per match at the moment) is " << m_salary << endl;
			cout << "The Months Length (TODO: years at the moment) is " << m_months << endl;
			cout << "The Monthly Penalty for premature interruption (TODO: unused at the moment) is " << m_penalty << endl;
			cout << "\nThe contract offer is going to be analyzed by the Player...\n\n";
			break;
		};
#else
		switch(m_phase)
		{
		case ENTRYGIFT:
			cout << "Enter the Offer: ";
			break;

		case NUM_PHASES:
			cout << "The Offer Is " << m_gift << endl;
			cout << "\nThe contract offer is going to be analyzed by the Player...\n\n";
			break;
		};
#endif
	}

	if(jumpError || m_phase == NUM_PHASES)
	{
		cout << Print::Options("\n0) Return to previous menu'");
		cout << "Selection: ";
		m_readyToGo = true;
	}
}

bool OfferPlayerMenu::Input(const STL::string& i_input)
{
	bool goodInput = true;
	const int res = Utilities::StringToInt(i_input);

	if(m_readyToGo)
	{
		switch(res)
		{
		case 0:
			GetMM()->PopMenu();
			break;
		};
	}
	else
	{
#ifndef DARIO
		switch(m_phase)
		{
		case ENTRYGIFT:
			m_gift = res;
			++m_phase;
			break;

		case SALARY:
			m_salary = res;
			++m_phase;
			break;

		case MONTHS:
			m_months = res;
			++m_phase;
			break;

		case PENALTY:
			m_penalty = res;
			// incomplete, we are not using gift and penalty and there's no "month" in current gameplay
			GetSP()->GetAuctionMgr().AddBet(Bet(m_userTeam, m_player, m_salary, m_months));
			++m_phase;
			break;
		};
#else
		switch(m_phase)
		{
		case ENTRYGIFT:
			m_gift = res;
			GetSP()->GetAuctionMgr().AddBet(Bet(m_userTeam, m_player, m_gift));
			++m_phase;
			break;
		};
#endif
	}

	return goodInput;
}